// pages/CircleFriends/CircleFriends.js
import regeneratorRuntime from '../../lib/runtime/runtime';

Page({
  data: {
    user_id: null,
    user_name: null,
    tipLogId: null,
    tipIndex: null,
    commentShow: false, 
    commentValue: "",
    commentContent: "",
    friends:[],
    photoWidth: wx.getSystemInfoSync().windowWidth / 5,
    popTop: 0, //弹出点赞评论框的位置
    popWidth: 0, //弹出框宽度
    isShow: true, //判断是否显示弹出框
  },

  onLoad: function(options) {
    let user = wx.getStorageSync('user')
    this.setData({
      user_id: user.user_id,
      user_name: user.user_name
    })
  },
  onShow: function() {
    this.getFriendsList();
  },
  //获取商品列表
  async getFriendsList() {
    var that = this;
    wx.request({
      url: 'http://localhost/gym/public/admin/api/log',
      // data: {
      //   cid: goodCid
      // },
      header: {
        "content-type": "application/json"
      },
      success(res) {
        let friends = res.data.message
        for (let i = 0; i < friends.length; i++) {
          if (friends[i].like) {
            for (let j = 0; j < friends[i].like.length; j++) {
              if (friends[i].like[j].user_id == that.data.user_id) {
                friends[i].isLike = true
              }
            }
          }
        }
        that.setData({ friends: friends});
      }
    })
    //关闭下啦刷新窗口
    wx.stopPullDownRefresh();
  },
  //   var that = this;
  //   wx.request({
  //     url: 'http://localhost/gym/public/admin/api/log',
  //       success(res){
  //         that.setData({
  //           friends: result.data.message
  //           });
  //         // friends
  //     }
  //   })
  // },
  // 点击图片进行大图查看
  LookPhoto: function(e) {
    var current = e.target.dataset.src
    wx.previewImage({
      current: current,
      urls: [current],
    })
  },
  // 跳转到发布朋友圈
  uplog: function(e) {
    wx.navigateTo({
      url: '/pages/uplog/index',
    })
  },
  zan: function(e) {
    let that = this
    let index = e.currentTarget.dataset.index
    let log_id = e.currentTarget.dataset.logId
    wx.request({
      url: 'http://localhost/gym/public/admin/api/zan',
      data: {
        log_id: log_id,
        user_id: that.data.user_id
      },
      success(res) {
        console.log(res)
        let status = res.data.message
        let friends = that.data.friends
        if (status == 1) {
          friends[index].isLike = true;
          if (friends[index].like) {
            friends[index].like.push({
              user_id: that.data.user_id,
              user_name: that.data.user_name
            })
          }else {
            friends[index].like = [{
              user_id: that.data.user_id,
              user_name: that.data.user_name
            }]
          }
          
          that.setData({
            friends: friends
          })
          wx.showToast({
            title: '点赞成功',
          })
        }
        if (status == 0) {
          friends[index].isLike = false;
          for (let i = 0; i < friends[index].like.length; i++) {
            if (friends[index].like[i].user_id == that.data.user_id) {
              friends[index].like.splice(i, 1);
            }
          }
          that.setData({
            friends: friends
          })
          wx.showToast({
            title: '取消赞成功',
          })
        }
      }
    })
  },
  tipComment: function(e) {
    let that = this
    that.setData({
      commentShow: true,
      tipLogId: e.currentTarget.dataset.logId,
      tipIndex: e.currentTarget.dataset.index,
      commentValue: ""
    })
  },
  comment: function(e){
    let that = this
    wx.showLoading({
      title: '正在上传',
    })
    let index = that.data.tipIndex
    let log_id = that.data.tipLogId
    let commentContent = that.data.commentContent
    wx.request({
      url: 'http://localhost/gym/public/admin/api/comment',
      data: {
        log_id: log_id,
        user_id: that.data.user_id,
        comment: commentContent
      },
      success(res) {
        console.log(res)
        let friends = that.data.friends
        if (friends[index].comment) {
          friends[index].comment.push({
            user_id: that.data.user_id,
            user_name: that.data.user_name,
            comment_desc: commentContent
          })
        } else {
          friends[index].comment = [{
            user_id: that.data.user_id,
            user_name: that.data.user_name,
            comment_desc: commentContent
          }]
        }
        that.setData({
          friends: friends,
          tipLogId: null,
          tipIndex: null,
          commentShow: false,
          commentValue: ""
        })
        wx.showToast({
          title: '评论成功',
        })
      },
      fail: function(e) {
        wx.showToast({
          title: '网络连接失败',
          icon:"none"
        })
      }
    })
  },
  commentInput: function(e) {
    let that = this
    that.setData({
      commentContent: e.detail.value
    })
  },
  commentBlur: function(e) {
    let that = this
    that.setData({
      commentShow: false,
      commentValue: ""
    })
  },
  // 删除朋友圈
  delete1: function(e) {
    let that = this
    let index = e.currentTarget.dataset.index
    let log_id = e.currentTarget.dataset.logId
    wx.request({
      url: 'http://localhost/gym/public/admin/api/delete1',
      data: {
        log_id: log_id  
      },
      success(res) {
        let friends = that.data.friends
          friends.splice(index, 1);
          that.setData({
            friends: friends
          })
          wx.showToast({
            title: '删除成功',
          })
      }
    })
  }
})